
$(document).ready(function(){
	
	wantBanner();
	pageBannerChoose();
	pageChooseTime();
	
	wantCity(1);
	setCity();
	clickcity();
	PageLoadTime();
	ChooseRentTime();
	WantSixCityInfo();
	pageBannerjs();
});
//计算出发返程时间差
function CountRentTime(){
	var startdate = $("input#chooseGotime").val();
	var enddate = $("input#chooseBacktime").val();
	var starthour = $("input#chooseGohour").val();
	var endhour = $("input#chooseBackhour").val();
	startdate = startdate.replace(/-/g,"/");
	enddate = enddate.replace(/-/g,"/");
	var sd = startdate + " " + starthour + ":00";
	var ed = enddate + " " + endhour + ":00";
	//处理时间
	var stime = new Date(sd);
	var etime = new Date(ed);
	var usedTime = etime - stime;
	var days=Math.floor(usedTime/(24*3600*1000));  
    //计算出小时数  
    var leave1=usedTime%(24*3600*1000);    //计算天数后剩余的毫秒数  
    var hours=Math.floor(leave1/(3600*1000));  
    //计算相差分钟数  
    var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数  
    var minutes=Math.floor(leave2/(60*1000));  
	
	var up = days + "天";
	var down = hours + "小时" + minutes + "分钟";
	$("div.rentindex_choosemode_inside_up_time_three_time_up").text(up);
	$("div.rentindex_choosemode_inside_up_time_three_time_down").text(down);
	
}
//首页选择时间功能
function ChooseRentTime(){
	laydate.render({
		  elem: '#chooseGotime', //指定元素,
		  min:new Date().Format("yyyy-MM-dd")
	});
	laydate.render({
		  elem: '#chooseBacktime', //指定元素,
		  min:new Date().Format("yyyy-MM-dd")
	});
	$("input#chooseGohour").click(function(e){
		e.stopPropagation();
		$("div.rentindex_choosemode_one_right_timechoose").removeClass("hidden");
		$("div.rentindex_choosemode_one_right_timechoose").focus();
		$("div.rentindex_choosemode_one_right_timechoosereturn").addClass("hidden");
	});
		//绑定选择出发时间事件
		for(var i=0;i<48;i++){
			$("div.rentindex_choosemode_one_right_timechoose div.index_choosemode_one_right_timechoose_right_item:eq("+i+")").click(function(i){
				return function(){
					var time = $(this).text();
//					starthour = $(this).data("time");
					$("input#chooseGohour").val(time);
					$("div.rentindex_choosemode_one_right_timechoose").addClass("hidden");
					CountRentTime();
				}
			}(i))
		};
	
	$("input#chooseBackhour").click(function(e){

		$("div.rentindex_choosemode_one_right_timechoosereturn").removeClass("hidden");
		$("div.rentindex_choosemode_one_right_timechoosereturn").focus();
		$("div.rentindex_choosemode_one_right_timechoose").addClass("hidden");
		e.stopPropagation();
		// e.stopPropagation();
	});
		//绑定选择返回时间事件
		for(var i=0;i<48;i++){
			$("div.rentindex_choosemode_one_right_timechoosereturn div.index_choosemode_one_right_timechoose_right_item:eq("+i+")").click(function(i){
				return function(){
					var etime = $(this).text();
//					endhour = $(this).data("time");
					$("input#chooseBackhour").val(etime);
					$("div.rentindex_choosemode_one_right_timechoosereturn").addClass("hidden");
					CountRentTime();
				}
			}(i))
		};
		
	

	$("div.rentindex_choosemode").mouseenter(function(){
		CountRentTime();
	});
	//设置title
	$(document).attr("title","搜房车 | 领先的房车自驾游租车平台，300+城市门店，5000+辆自动档房车，C照轻松租");//修改title值  
}

//请求banner接口
function wantBanner(){
	$.ajax({
		dataType : "json",
		type : "get",
		url : basePath+"/bannar",
		data : {
			
		},
		contentType : "application/json",

		success : function(data) {
				
			for (var i = 0; i < data.data.length; i++) {
				var a=data.data[i].picture;
				var b=i+1;
				$("img#bannerimg"+b).attr("src",a);
			};
			console.log("banner");
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
			layer.close(LoadingIcon);
		},
	});
}
//立即选车跳转
function pageChooseTime(){
	//立即选车跳转
	$("input#chooseTimenow").click(
			function() {
				var test = TestGoToList();
				if(test == 1){
					var startDate = $("input#chooseGotime").val();
					var endDate = $("input#chooseBacktime").val();
					var rentcity = $("input#chooseGocity").val();
					var starthour = $("input#chooseGohour").val();
					var endhour = $("input#chooseBackhour").val();
					var url = basePath+"/rentlist?rentcity="+rentcity+"&startDate="+startDate+"&endDate="+endDate+"&starthour="+starthour+"&endhour="+endhour;
					url = encodeURI(url);
					url = encodeURI(url);
					window.location.href = (url);
				};	
	});
}
//banner功能栏的选择效果,点击切换css及对应div出现
function pageBannerChoose(){
	// banner功能栏的选择效果,点击切换css及对应div出现
	$("div.index_choosemode_left>div:eq(0)").click(
			function() {
				$(this).attr("class",
						"index_choosemode_left_item_choose");
				$("div.index_choosemode_left>div").not(this)
						.attr("class",
								"index_choosemode_left_item");
				$("div.index_choosemode_right>div:eq(0)").css(
						"display", "block");
				$("div.index_choosemode_right>div").not(
						"div.index_choosemode_right>div:eq(0)")
						.css("display", "none");
			});
	$("div.index_choosemode_left>div:eq(1)").click(
			function() {
				$(this).attr("class",
						"index_choosemode_left_item_choose");
				$("div.index_choosemode_left>div").not(this)
						.attr("class",
								"index_choosemode_left_item");
				$("div.index_choosemode_right>div:eq(1)").css(
						"display", "block");
				$("div.index_choosemode_right>div").not(
						"div.index_choosemode_right>div:eq(1)")
						.css("display", "none");
			});
	$("div.index_choosemode_left>div:eq(2)").click(
			function() {
				$(this).attr("class",
						"index_choosemode_left_item_choose");
				$("div.index_choosemode_left>div").not(this)
						.attr("class",
								"index_choosemode_left_item");
				$("div.index_choosemode_right>div:eq(2)").css(
						"display", "block");
				$("div.index_choosemode_right>div").not(
						"div.index_choosemode_right>div:eq(2)")
						.css("display", "none");
			});
	$("div.index_choosemode_left>div:eq(3)").click(
			function() {
				$(this).attr("class",
						"index_choosemode_left_item_choose");
				$("div.index_choosemode_left>div").not(this)
						.attr("class",
								"index_choosemode_left_item");
				$("div.index_choosemode_right>div:eq(3)").css(
						"display", "block");
				$("div.index_choosemode_right>div").not(
						"div.index_choosemode_right>div:eq(3)")
						.css("display", "none");
			});
}
//banner房车租赁js
function pageBannerjs(){
	
	$("#chooseGocity").click(
			function(e) {
				e.stopPropagation();
				$(".rentindex_choosemode_one_right_city").removeClass("hidden");
				$(".rentindex_choosemode_one_right_city").focus();
			}); // 选择城市得到焦点触发
	$("#chooseBackcity").click(
			function(e) {
				e.stopPropagation();
				$(".rentindex_choosemode_one_right_city").removeClass("hidden");
				$(".rentindex_choosemode_one_right_city").focus();
			}); // 选择城市得到焦点触发
	
}
//首页banner中房车租赁城市请求
function wantCity(type){
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/carsearch",
		data : {
			type : type
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",

		success : function(data) {
			var s = "";
			for (var i = 0; i < data.data.length; i++) {
				s = s+"<div class=\"rentindex_choosemode_one_right_city_down_item\">"+data.data[i].city+"</div>"
					 +"<font class=\"hidden\">"+data.data[i].city+"</font>"						
				
			};
			$("div.rentindex_choosemode_one_right_city_down").html(s);
			setCity();//只有在生成结构之后才能对结构进行绑定事件
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}
//绑定banner城市选择点击事件
function setCity(){
	var len=$("div.rentindex_choosemode_one_right_city_down_item").length;
	for(var i=0;i<len;i++){
		$("div.rentindex_choosemode_one_right_city_down_item:eq("+i+")").click(function(i){
			return function(e){
				e.stopPropagation();
				var city=$("div.rentindex_choosemode_one_right_city_down>font:eq("+i+")").text();
				$("input#chooseGocity").val(city);
				$("input#chooseBackcity").val(city);
				$("div.rentindex_choosemode_one_right_city").addClass("hidden");
			}		
		}(i))
	}

	$(document).on('click',function(e){
		e.stopPropagation();
		$('.rentindex_choosemode_one_right_timechoose').addClass("hidden");
		$('.rentindex_choosemode_one_right_timechoosereturn').addClass("hidden");
		$("div.rentindex_choosemode_one_right_city").addClass("hidden");
	})
}
//绑定banner选择多少城市点击事件
function clickcity(){
	for(var i=0;i<6;i++){
		$("div.rentindex_choosemode_one_right_city_up_three_item:eq("+i+")").click(function(i){
			return function(e){
				e.stopPropagation();
				$(this).addClass("rentindex_choosemode_one_right_city_up_three_itemchoose");
				// $("div.rentindex_choosemode_one_right_city").removeClass("hidden");
				$("div.rentindex_choosemode_one_right_city_up_three>div").not(this).removeClass("rentindex_choosemode_one_right_city_up_three_itemchoose");
				var type=i+1;
				wantCity(type);
			}			
		}(i));
	}
}
//初始化出发时间返程时间
function PageLoadTime(){
$.ajax({		
		dataType : "json",
		type : "post",
		url : basePath+"/indexFormaltime",
		data : {
			
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var start = data.data.start;
			var end = data.data.end;
			$("input#chooseGotime").val(start);
			$("input#chooseBacktime").val(end);
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}
//点击城市跳转到租车列表页
function GoTolist(){
	var len = $("div.rentindex_city_content_right_item").length;
	for(var i =0;i<len;i++){
		$("div.rentindex_city_content_right_item:eq("+i+")").click(function(i){
			return function(){
				var test = TestGoToList();
				if(test == 1){
					var startDate = $("#chooseGotime").val();
					var endDate = $("#chooseBacktime").val();
					var rentcity=$("div.rentindex_city_content_right_item_city:eq("+i+")").text();
					var starthour = $("input#chooseGohour").val();
					var endhour = $("input#chooseBackhour").val();
					var url = basePath+"/rentlist?rentcity="+rentcity+"&startDate="+startDate+"&endDate="+endDate+"&starthour="+starthour+"&endhour="+endhour;
					url = encodeURI(url);
					url = encodeURI(url);
					window.location.href = (url);
				};	
			}
		}(i));
	};
}
//请求6个城市信息
function WantSixCityInfo(){
	$.ajax({		
		dataType : "json",
		type : "post",
		url : basePath+"/car/city",
		data : {
			
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var c = "";
			var len = (data.data.length>6?6:data.data.length);
			for(var i =0;i<len;i++){
				c = c + "<div class=\"col-lg-4 col-md-4 rentindex_city_content_right_item\">"+
							"<div class=\"rentindex_city_content_right_item_icon\"><img src=\""+data.data[i].picture+"\" class=\"img-responsive\" /></div>"+
							"<div class=\"rentindex_city_content_right_item_city\">"+data.data[i].city+"</div>"+
							"<font class=\"hidden\"></font>"+
						"</div>"
			};
			$("div.rentindex_city_content_right").html(c);
			GoTolist();
			console.log("len="+len);
			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}
//是否可以跳转判定
function TestGoToList(){
	var startdate = $("input#chooseGotime").val();
	var enddate = $("input#chooseBacktime").val();
	var starttime = $("input#chooseGohour").val();
	var endtime = $("input#chooseBackhour").val();
	startdate = startdate.replace(/-/g,"/");
	enddate = enddate.replace(/-/g,"/");
	var start = startdate + " " + starttime + ":00";
	var end = enddate + " " + endtime + ":00";
	var sd = new Date(start);
	var ed = new Date(end);
	var now = new Date();
	console.log("start="+start);
	console.log("sd="+sd);
	console.log("end="+end);
	console.log("ed="+ed);
	if(sd >= ed){
		alert("选择开始时间不能大于结束时间！");
		return(0);
	};
	if(sd <= now){
		alert("选择开始时间不能小于现在时间！")
		return(0)
	};
	return(1);		
}

